<?php

/**
 * This is the model class for table "pet".
 *
 * The followings are the available columns in table 'pet':
 * @property string $pet_id
 * @property string $user_id
 * @property string $nickname
 * @property string $type
 * @property string $race
 * @property string $birthdate
 * @property string $color
 * @property string $signs
 * @property string $character
 * @property string $toys
 * @property string $games
 * @property string $like
 * @property string $dislike
 * @property string $awards
 * @property integer $castrated
 * @property integer $reproduction
 * @property string $place
 * @property string $about
 * @property string $avatar
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Petphotoalbum[] $petphotoalbums
 */
class Pet extends CActiveRecord
{
    public $avatar;
    /**
     * Returns the static model of the specified AR class.
     * @return Pet the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'pet';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, nickname, type, race', 'required'),
            array('castrated, reproduction', 'numerical', 'integerOnly'=>true),
            array('user_id', 'length', 'max'=>10),
            array('nickname', 'length', 'max'=>100),
            array('type', 'length', 'max'=>100),
            array('race', 'length', 'max'=>100),
            array('color', 'length', 'max'=>200),
            array('birthdate', 'validateDate'),
            array('signs, character, toys, games, like, dislike, awards, place, about, birthdate', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('pet_id, user_id, nickname, type, race, birthdate, color, signs, character, toys, games, like, dislike, awards, castrated, reproduction, place, about', 'safe', 'on'=>'search'),
        );
    }

    public function validateDate()
    {
        if (!strtotime($this->birthdate))
        {
            $this->birthdate = null;
        }
    }


    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
            'petphotoalbums' => array(self::HAS_MANY, 'Petphotoalbum', 'pet_id'),
            'avatar' => array(self::HAS_ONE, 'Uploadimage', 'image_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'pet_id' => 'Любимец',
            'user_id' => 'Пользователь',
            'nickname' => 'Имя',
            'type' => 'Вид',
            'race' => 'Порода',
            'birthdate' => 'День рожденья',
            'color' => 'Окрас',
            'signs' => 'Приметы',
            'character' => 'Характер',
            'toys' => 'Любимые игрушки',
            'games' => 'Игры',
            'like' => 'Любит',
            'dislike' => 'Не любит',
            'awards' => 'Награды',
            'castrated' => 'Стерилизован/ кастрирован',
            'reproduction' => 'Готов к размножению',
            'place' => 'Место жилья',
            'about' => 'О Любимце',
            'avatar' => 'Фото'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('pet_id',$this->pet_id,true);
        $criteria->compare('user_id',$this->user_id,true);
        $criteria->compare('nickname',$this->nickname,true);
        $criteria->compare('type',$this->type,true);
        $criteria->compare('race',$this->race,true);
        $criteria->compare('birthdate',$this->birthdate,true);
        $criteria->compare('color',$this->color,true);
        $criteria->compare('signs',$this->signs,true);
        $criteria->compare('character',$this->character,true);
        $criteria->compare('toys',$this->toys,true);
        $criteria->compare('games',$this->games,true);
        $criteria->compare('like',$this->like,true);
        $criteria->compare('dislike',$this->dislike,true);
        $criteria->compare('awards',$this->awards,true);
        $criteria->compare('castrated',$this->castrated);
        $criteria->compare('reproduction',$this->reproduction);
        $criteria->compare('place',$this->place,true);
        $criteria->compare('about',$this->about,true);        

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
        ));
    }
    
    public function scopes()
        {
            return array(
                'latest'=>array(
                    'order' =>  'pet_id DESC',
                    'limit' =>  3,
                ),                
            );
        }
}